import Icon from './icon'
class Loading {

    constructor({ text, background, textColor }) {
        this.text = text || "加载中...";
        this.background = background || 'rgba(0, 0, 0, 0.8)';
        this.textColor = textColor || '#409eff'
        this.init();
    }
    init() {
        let element = document.createElement('div');
        element.className = 'loading';

        element.classList.add('fadeIN-loading');
        element.style.background = this.background;

        let icon = new Icon(this.textColor).getElement();
        let box = document.createElement('div');
        let text = document.createElement('div');

        box.className = 'box';

        text.className = 'text';
        text.style.color = this.textColor;
        text.innerText = this.text;
        box.appendChild(icon);
        box.appendChild(text);

        element.appendChild(box);
        this.element = element;
        document.body.appendChild(element)
        this._HtmlHiddenScroll();
    }
    close() {
        this.element.classList.remove('fadeIN-loading');
        this.element.classList.add('fadeOUT-loading');
        setTimeout(() => {
            document.body.removeChild(this.element);
            this._HtmlVisibleScroll();
        }, 200);

    }

    // 显示滚动条
    _HtmlVisibleScroll() {
        let html = document.getElementsByTagName('html')[0];
        let body = document.body;
        html.style.overflow = "visible";
        body.style.overflow = "visible";
    }
    // 隐藏滚动条
    _HtmlHiddenScroll() {
        let html = document.getElementsByTagName('html')[0];
        let body = document.body;
        html.style.overflow = "hidden";
        body.style.overflow = "hidden";
    }
}

export default Loading;